import React from "react";

import BaseComponent from "../../../../redare/components/BaseComponent";
import PermissionPresenter from "../../.gen/presenter/component/core/PermissionPresenter";
import {Button, Checkbox, Row} from "antd";
import Grid from "../../../../redare/components/grid/Grid";
import * as StringUtils from "../../../../redare/utils/StringUtils";
import FunPermission from "./FunPermission";
import {getAbsolutePosition} from "../../../../redare/utils/DocumentUtils";

export default class RoleFunGrid extends BaseComponent {

    constructor(props) {
        super(props);
        this.presenter = new PermissionPresenter(this);
        this._domId = `${this.props.platform}-role-fun-grid`;
    }

    componentDidMount() {
        let grid = document.getElementById(this._domId);
        let {top} = getAbsolutePosition(grid);
        if (top) {
            this.setState({offsetTop: top + 70});
        } else {
            this.setState({offsetTop: 200});
        }
    }

    setRoleId(roleId) {
        this._form.roleId = roleId;
        this._form.menuId = null;
        this._findFunList();
    }

    setMenuId(menuId) {
        this._form.menuId = menuId;
        this._findFunList();
    }

    _findFunList() {
        let {roleId, menuId} = this._form;
        if (roleId && menuId) {
            this.showLoadingIndicator('加载中...');
            this.presenter.findRoleFunList({roleId, menuId}, {returnAll: true}, (result) => {
                this.grid.setData(result.data);
            })
        } else {
            this.grid.setData(null);
        }
    }

    _onPermissionChange(record) {
        let {id, adminPermission, hasPermission} = record;
        if (adminPermission) {
            this.toastInfo('管理员角色具有全部权限，不能修改');
            return;
        }
        let {roleId} = this._form;
        if (hasPermission) {
            this.presenter.delRoleFun({roleId, funIds: [id]}, null, () => {
                this.noticeSuccess('操作成功');
                this._findFunList();
            });
        } else {
            this.presenter.addRoleFun({roleId, funIds: [id]}, null, () => {
                this.noticeSuccess('操作成功');
                this._findFunList();
            });
        }
    }

    showLoadingIndicator(text, options = {}) {
        this.grid.showLoadingIndicator(text);
    }

    closeLoadingIndicator() {
        this.grid.closeLoadingIndicator();
    }


    render() {
        return (
            <>
                <Row style={({paddingBottom: 10})}>
                    <Button onClick={this._findFunList.bind(this)}>刷新</Button>
                </Row>
                <Grid
                    ref={(o) => {
                        this.grid = o
                    }}
                    id={this._domId}
                    scroll={{y: window.document.body.clientHeight - this.state.offsetTop || 0}}
                    _showPage={false}
                    _initLoading={false}
                    rowKey={(r) => r.id}
                    size="small"
                    columns={[
                        {
                            title: '功能名称（分类）',
                            dataIndex: 'name',
                            render: (value, record) => {
                                let {name, module, classifyLevel} = record;
                                module = StringUtils.isBlank(module) ? '系统模块' : module;
                                classifyLevel = StringUtils.isBlank(classifyLevel) ? classifyLevel : classifyLevel.replace(/,/ig, ' > ');
                                return (
                                    <div>
                                        <b>{name}</b><br/>
                                        <i>{module}&nbsp;&gt;&nbsp;{classifyLevel}</i>
                                    </div>
                                )
                            }
                        },
                        {
                            title: '权限',
                            dataIndex: 'permission',
                            align: 'center',
                            width: 80,
                            render: (value, record) => {
                                let {hasPermission} = record;
                                return (
                                    <FunPermission
                                        funId={['WebPlatformRolePermissionAddFun', 'WebPlatformRolePermissionDelFun']}
                                        funIdAll={true}
                                        noPermissionComponent={
                                            <Checkbox checked={hasPermission}
                                                      disabled={true}
                                            />
                                        }
                                    >
                                        <div onClick={(event) => {
                                            event.stopPropagation();
                                        }} style={({width: '100%', height: '100%'})}>
                                            <Checkbox checked={hasPermission}
                                                      onChange={this._onPermissionChange.bind(this, record)}
                                            />
                                        </div>
                                    </FunPermission>
                                );
                            }
                        }
                    ]}/>
            </>
        );
    }
}
